home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
EnigmA Amiga Run 1995 October
/
EnigmA AMIGA RUN 01 (1995)(G.R. Edizioni)(IT)[!][issue 1995-10][Aminet 7].iso
/
Enigma Amiga CD
/
Listati
/
64-Maggio-Listato1.c
< prev
next >
Wrap
C/C++ Source or Header
|
1995-04-08
|
8KB
|
219 lines
/****************************************************************************
* Listato 1 *
* Apre una finestra sul WorkBench di tipo SuperBitMap e GimmeZeroZero; la *
* bitmap della finestra è 1024x1024, e si disegnano una serie di linee. *
* Il programma attende che l'utente prema uno dei tasti cursore per sopstare*
* la bitmap; controlla i limiti nel caso venga cambiata la grandezza della *
* finestra; controlla il gadget di chiusura per l'uscita dal programma. *
****************************************************************************/
#define INTUI_V36_NAMES_ONLY
/* inclusione file di supporto */
#include <exec/types.h>
#include <exec/memory.h>
#include <intuition/intuition.h>
#include <intuition/screens.h>
#include <graphics/gfx.h>
#include <clib/exec_protos.h>
#include <clib/intuition_protos.h>
#include <clib/graphics_protos.h>
#include <clib/layers_protos.h>
#include <clib/dos_protos.h>
struct Library *IntuitionBase = NULL; /* puntatore a intuition.library */
struct Library *GfxBase = NULL; /* puntatore a graphics.library */
struct Library *LayersBase = NULL; /* puntatore a layers.library */
struct Screen *pubscreen = NULL; /* puntatore al workbench */
struct Window *finestra = NULL; /* puntatore alla finestra */
struct BitMap *supBitMap = NULL; /* puntatore alla bitmap */
struct MsgPort *UPort; /* puntatore alla porta IDCMP */
/* definizione prototipi di funzione */
void WaitEvent(struct MsgPort *,struct IntuiMessage *);
void CloseAll(void);
void OpenAll(void);
void DisegnaTutto(void);
UWORD RangeRand(unsigned long MaxValue); /* funzione di Amiga.lib per la generazione di numeri casuali */
void WaitEvent(struct MsgPort *porta,struct IntuiMessage *mess)
{
struct IntuiMessage *msg;
while ((msg = (struct IntuiMessage *)GetMsg(porta)) == NULL)
WaitPort(porta);
CopyMem(msg,mess,sizeof(struct IntuiMessage));
return;
}
/* procedura CloseAll() */
void CloseAll()
{
register ULONG i;
if (finestra != NULL) CloseWindow(finestra);
for (i=0; i<pubscreen->BitMap.Depth; i++)
if (supBitMap->Planes[i] != NULL) FreeRaster(supBitMap->Planes[i],1024,1024);
if (supBitMap != NULL) FreeMem(supBitMap,sizeof(struct BitMap));
if (pubscreen != NULL) UnlockPubScreen(NULL,pubscreen);
if (LayersBase != NULL) CloseLibrary(LayersBase);
if (GfxBase != NULL) CloseLibrary(GfxBase);
if (IntuitionBase != NULL) CloseLibrary(IntuitionBase);
exit(0);
}
/* procedura OpenAll() */
void OpenAll()
{
register ULONG i;
/* apriamo intuition.library, almeno version 36 */
if ((IntuitionBase = OpenLibrary("intuition.library",36L)) == NULL)
CloseAll();
/* apriamo graphics.library, almeno version 36; vengono utilizzate delle
funzioni per il disegno */
if ((GfxBase = OpenLibrary("graphics.library",36L)) == NULL)
CloseAll();
/* apriamo layers.library, almeno version 36; necessita per l'utilizzo di una
funzione per lo scrolling del layer della finestra */
if ((LayersBase = OpenLibrary("layers.library",36L)) == NULL)
CloseAll();
/* blocca lo schermo pubblico di default */
if ((pubscreen = LockPubScreen(NULL)) == NULL)
CloseAll();
/* alloca la memoria per la struttura BitMap */
if ((supBitMap = (struct BitMap *)AllocMem(sizeof(struct BitMap),MEMF_PUBLIC|MEMF_CLEAR)) == NULL)
CloseAll();
/* inizializza i campi della BitMap */
InitBitMap(supBitMap,pubscreen->BitMap.Depth,1024,1024);
/* alloca i piani di bit della BitMap */
for (i=0; i<pubscreen->BitMap.Depth; i++)
{
if ((supBitMap->Planes[i] = AllocRaster(1024,1024)) == NULL)
CloseAll();
}
/* apre finestra spostabile, ridimensionabile, SuperBitMap, GimmeZeroZero,
con il gadget di chiusura */
if ((finestra = OpenWindowTags(NULL,WA_Left,10,
WA_Top,10,
WA_Width,350,
WA_Height,150,
WA_MaxWidth,1024,
WA_MaxHeight,1024,
WA_Title,"Mostra BitMap - usare i tasti freccia",
WA_DragBar,TRUE,
WA_CloseGadget,TRUE,
WA_DepthGadget,TRUE,
WA_SizeGadget,TRUE,
WA_SizeBRight,TRUE,
WA_SizeBBottom,TRUE,
WA_Flags,WFLG_SUPER_BITMAP|WFLG_GIMMEZEROZERO|WFLG_NOCAREREFRESH,
WA_IDCMP,IDCMP_CLOSEWINDOW|IDCMP_RAWKEY|IDCMP_NEWSIZE,
WA_PubScreen,pubscreen,
WA_SuperBitMap,supBitMap,
TAG_END)) == NULL)
CloseAll();
UPort = finestra -> UserPort;
return;
}
/* Questa procedura disegna una serie di linee nella BitMap della finestra */
void DisegnaTutto()
{
register WORD x1,y1,x2,y2,penna,ncol,dx,dy;
ncol = 1 << pubscreen->BitMap.Depth;
dx = RangeRand(6)+2;
dy = RangeRand(6)+2;
penna = RangeRand(ncol-1)+1;
SetAPen(finestra->RPort,penna); /* imposta il colore da usare per il disegno */
x2 = 1023; y1 = 0; y2 = 1023;
for (x1=0; x1<1204; x1+=dx)
{
Move(finestra->RPort,x1,y1);
Draw(finestra->RPort,x2,y2); /* disegna la linea */
x2-=dx;
}
penna = RangeRand(ncol-1)+1;
SetAPen(finestra->RPort,penna);
x1 = 0; x2 = 1023; y2 = 1023;
for (y1=0; y1<1204; y1+=dy)
{
Move(finestra->RPort,x1,y1);
Draw(finestra->RPort,x2,y2);
y2-=dy;
}
return;
}
void main()
{
register LONG px=0,py=0,pp;
struct IntuiMessage mm;
OpenAll(); /* apre tutte le strutture dati */
SetDrMd(finestra->RPort,JAM1); /* cancella la BitMap */
SetAPen(finestra->RPort,0); RectFill(finestra->RPort,0,0,1023,1023);
DisegnaTutto(); /* disegna le linee */
/* sposta il layer nella posizione 0,0 */
pp = finestra->RPort->Layer->Scroll_X; /* determina la posizione X del layer */
ScrollLayer(0,finestra->RPort->Layer,-pp,0); /* sposta il layer di -X */
pp = finestra->RPort->Layer->Scroll_Y; /* stesso per Y */
ScrollLayer(0,finestra->RPort->Layer,0,-pp);
while(1)
{
WaitEvent(UPort,&mm); /* attende un evento */
if (mm.Class == IDCMP_CLOSEWINDOW) break; /* se gadget di chiusura esci */
else if (mm.Class == IDCMP_RAWKEY)
{
if (mm.Code == 0x4C) /* tasto freccia su */
{
pp = py;
py -= 16;
if (py<0) py = 0;
ScrollLayer(0,finestra->RPort->Layer,0,py-pp);
}
else if (mm.Code == 0x4D) /* tasto freccia giu */
{
pp = py;
py += 16;
if (py>(1024-finestra->GZZHeight)) py = 1024-finestra->GZZHeight;
ScrollLayer(0,finestra->RPort->Layer,0,py-pp);
}
else if (mm.Code == 0x4F) /* tasto freccia sinistra */
{
pp = px;
px -= 16;
if (px<0) px = 0;
ScrollLayer(0,finestra->RPort->Layer,px-pp,0);
}
else if (mm.Code == 0x4E) /* tasto freccia destra */
{
pp = px;
px += 16;
if (px>(1024-finestra->GZZWidth)) px = 1024-finestra->GZZWidth;
ScrollLayer(0,finestra->RPort->Layer,px-pp,0);
}
}
else if (mm.Class == IDCMP_NEWSIZE) /* l'utente ha cambiato dimensione, verificare la corretta posizione */
{
pp = px;
if (px>(1024-finestra->GZZWidth)) px = 1024-finestra->GZZWidth;
ScrollLayer(0,finestra->RPort->Layer,px-pp,0);
pp = py;
if (py>(1024-finestra->GZZHeight)) py = 1024-finestra->GZZHeight;
ScrollLayer(0,finestra->RPort->Layer,0,py-pp);
}
}
CloseAll(); /* chiudi tutto ed esci */
}